/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.services;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

/**
 * A simple {@link Runnable task} that sets up the alarm. 
 */
public class SetAlarmTask implements Runnable{

	private final long _date;
	private final AlarmManager _am;
	private final Context _context;
	private final String _identifier;
	
	public SetAlarmTask(Context context, long date, String identifier){
		this._context=context;
		this._am=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		this._date=date;
		this._identifier=identifier;
	}
	@Override
	public void run() {
		 // Request to start are service when the alarm date is upon us
        // We don't start an activity as we just want to pop up a notification into the system bar not a full activity
        Intent intent = new Intent(_context, NotifyService.class);
        intent.putExtra(NotifyService.INTENT_NOTIFY, true);
        intent.setData(Uri.parse(_identifier));
        PendingIntent pendingIntent = PendingIntent.getService(_context, 0, intent, 0);
        _am.cancel(pendingIntent);
        // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
        _am.set(AlarmManager.RTC, _date, pendingIntent);
	}

}
